package com.ddc.study.feature.friend;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import com.ddc.study.R;
import com.ddc.study.base.MvpActivity;
import com.ddc.study.entity.Friend;
import com.ddc.study.feature.html.HtmlActivity;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2018/6/7.
 *
 * @author lshun
 * @version 1.0
 */
public class FriendSiteActivity extends MvpActivity<FriendSiteContract.IView, FriendSiteContract.IPresenter>
        implements FriendSiteContract.IView {

    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.group_chip)
    ChipGroup mChipGroup;

    @BindView(R.id.layout_load_failed)
    View mLoadFailedView;

    @Override
    protected FriendSiteContract.IPresenter createPresenter() {
        return new FriendSitePresenter();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_friend;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        loadData();

        mSwipeRefreshLayout.setOnRefreshListener(this::loadData);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showData(@Nullable List<Friend> data) {
        if (mSwipeRefreshLayout.isRefreshing())
            mSwipeRefreshLayout.setRefreshing(false);

        if (data == null) {
            if (mChipGroup.getChildCount() == 0) {
                showLoadFailedView();
            } else {
                showToast(R.string.refresh_failed);
            }
            return;
        }

        showNormalView();

        mChipGroup.removeAllViews();
        Chip chip;
        for (final Friend friend : data) {
            chip = (Chip) View.inflate(this, R.layout.layout_chip, null);
            chip.setText(friend.getName());
            chip.setOnClickListener(view -> HtmlActivity.openUrl(FriendSiteActivity.this, friend.getLink()));
            mChipGroup.addView(chip);
        }
    }

    @OnClick(R.id.layout_load_failed)
    void loadData() {
        mPresenter.loadData();
        showLoadingView();
    }

    private void showNormalView() {
        mLoadFailedView.setVisibility(View.GONE);
        mSwipeRefreshLayout.setVisibility(View.VISIBLE);
        if (mSwipeRefreshLayout.isRefreshing())
            mSwipeRefreshLayout.setRefreshing(false);
    }

    private void showLoadingView() {
        mLoadFailedView.setVisibility(View.GONE);
        mSwipeRefreshLayout.setVisibility(View.VISIBLE);
        if (!mSwipeRefreshLayout.isRefreshing())
            mSwipeRefreshLayout.setRefreshing(true);
    }

    private void showLoadFailedView() {
        mLoadFailedView.setVisibility(View.VISIBLE);
        mSwipeRefreshLayout.setVisibility(View.GONE);
    }
}
